#include "faderwidget.h"
#include <QTimer>
#include <QPainter>

FaderWidget::FaderWidget(QWidget *parent)
    : QWidget(parent)
{
    if(parent)
        startColor=parent->palette().window().color();
    else
        startColor=Qt::white;

    currentAlpha = 0;
    duration = 1000;

    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()),this, SLOT(update()));

    setAttribute(Qt::WA_DeleteOnClose);
    resize(parent->size());

}

FaderWidget::~FaderWidget()
{
}

void FaderWidget::start()
{
    currentAlpha = 255;
    timer->start(10);
    show();
}

void FaderWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QColor   semiTransparentColor = startColor;
    semiTransparentColor.setAlpha(currentAlpha);
    painter.fillRect(rect(), semiTransparentColor);

    currentAlpha -=255*timer->interval()/duration;
    if(currentAlpha <= 0)
    {
        timer->stop();
        close();
    }
}
